#version 130

uniform sampler2DShadow map_shadow0;
uniform sampler2DShadow map_shadow1;
uniform sampler2DShadow map_shadow2;


in vec4 shadow_map_coord[3];

bool test (vec4 p)
{
        vec3 pr = p.xyz / p.w;
        return pr.x >= 0.0 && pr.x <= 1.0 && pr.y >= 0.0 && pr.y <= 1.0;
}

float calc_shadow_value ()
{
        float result = 1;
        if (test (shadow_map_coord[0]))
        {
                result = shadow2DProj (map_shadow0, shadow_map_coord[0]).x;
        }
        else if (test(shadow_map_coord[1]))
        {
                result = shadow2DProj (map_shadow1, shadow_map_coord[1]).x;
        }
        else if (test(shadow_map_coord[2]))
        {
                result = shadow2DProj (map_shadow2, shadow_map_coord[2]).x;
        }
        if (result < 0.5)
        {
                result = 0.5;
        }
        else
        {
                result = 1.0;
        }
        return result;
}

void main()
{
    float shadow_value = calc_shadow_value ();

    gl_FragColor = vec4 (shadow_value, shadow_value, shadow_value, 1.0);

}
